/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.washburn.cm333.project.gui;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

import edu.washburn.cm333.project.entities.Lease;
import edu.washburn.cm333.project.entities.Main;
import edu.washburn.cm333.project.entities.Property;
import edu.washburn.cm333.project.entities.PropertyManager;
import edu.washburn.cm333.project.entities.Tenant;

/**
 *
 * @author Midwest Health
 */
public class LeaseForm extends javax.swing.JFrame {

	/**
	 * Creates new form LeaseForm
	 */
	public LeaseForm() {
		initComponents();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		btnSubmit = new javax.swing.JButton();
		btnExit = new javax.swing.JButton();
		lblRent = new javax.swing.JLabel();
		lblLeaseLength = new javax.swing.JLabel();
		txtRent = new javax.swing.JTextField();
		txtLeaseLength = new javax.swing.JTextField();
		lblTitle = new javax.swing.JLabel();
		EntityManager em1 = PropertyManager.getEntityManager();
		List<Property> propertyList = em1.createQuery(
				"select t from Property t", Property.class).getResultList();
		cboxProperty = new Java2sAutoComboBox(propertyList);
		EntityManager em = PropertyManager.getEntityManager();
		List<Tenant> tenantList = em.createQuery("select t from Tenant t",
				Tenant.class).getResultList();
		cboxTenant = new Java2sAutoComboBox(tenantList);
		lblProperty = new javax.swing.JLabel();
		lblSelectTenant = new javax.swing.JLabel();

		setTitle("New Lease Form");

		btnSubmit.setText("Submit");
		btnSubmit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSubmitActionPerformed(evt);
			}
		});

		btnExit.setText("Exit");
		btnExit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnExitActionPerformed(evt);
			}
		});

		lblRent.setText("Rent");

		lblLeaseLength.setText("Lease Length");

		lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
		lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lblTitle.setText("Add New Lease");

		cboxProperty.setSelectedIndex(-1);
		cboxProperty.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cboxPropertyActionPerformed(evt);
			}
		});

		cboxTenant.setSelectedIndex(-1);
		cboxTenant.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cboxTenantActionPerformed(evt);
			}
		});

		lblProperty.setText("Select Property");

		lblSelectTenant.setText("Select Tenant");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE,
						316, Short.MAX_VALUE)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(33, 54, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btnSubmit)
																.addGap(65, 65,
																		65)
																.addComponent(
																		btnExit,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		67,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						lblLeaseLength)
																				.addComponent(
																						lblProperty)
																				.addComponent(
																						lblRent)
																				.addComponent(
																						lblSelectTenant))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																								.addComponent(
																										txtLeaseLength,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										125,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING)
																												.addComponent(
																														txtRent,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														125,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														cboxProperty,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														javax.swing.GroupLayout.DEFAULT_SIZE,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addComponent(
																						cboxTenant,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))))
								.addContainerGap(53, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addComponent(lblTitle,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										28,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblProperty)
												.addComponent(
														cboxProperty,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblSelectTenant)
												.addComponent(
														cboxTenant,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														txtRent,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(lblRent))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblLeaseLength)
												.addComponent(
														txtLeaseLength,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(btnSubmit)
												.addComponent(btnExit))
								.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void cboxPropertyActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		Object o = ((Java2sAutoComboBox) cboxProperty).autoTextFieldEditor
				.getItem();
		if (o instanceof Property)
			property = (Property) o;
	}

	private void cboxTenantActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		Object o = ((Java2sAutoComboBox) cboxTenant).autoTextFieldEditor
				.getItem();
		if (o instanceof Tenant)
			tenant = (Tenant) o;
	}

	private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.txtRent.getText().trim().matches("[0-9]+")
				&& this.txtLeaseLength.getText().trim().matches("[a-zA-Z0-9]+")
				&& property != null && tenant != null) {
			EntityManager em = PropertyManager.getEntityManager();

			em.getTransaction().begin();
			Lease lease = new Lease();
			lease.setProperty(property);
			lease.setTenants(tenant);
			lease.setRent(txtRent.getText());
			lease.setTermMonths(Integer.valueOf(txtLeaseLength.getText()));
			em.persist(lease);
			em.getTransaction().commit();

			this.setVisible(false);
		} else {
			JOptionPane
					.showMessageDialog(
							this,
							"All fields are required and must have the apropriate alpha or numeric characters.",
							"Missing or Incorrect Information",
							JOptionPane.WARNING_MESSAGE);
		}
	}

	private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
		this.setVisible(false);
	}//GEN-LAST:event_btnExitActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(LeaseForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(LeaseForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(LeaseForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(LeaseForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new LeaseForm().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnExit;
	private javax.swing.JButton btnSubmit;
	private javax.swing.JComboBox cboxProperty;
	private javax.swing.JComboBox cboxTenant;
	private javax.swing.JLabel lblLeaseLength;
	private javax.swing.JLabel lblProperty;
	private javax.swing.JLabel lblRent;
	private javax.swing.JLabel lblSelectTenant;
	private javax.swing.JLabel lblTitle;
	private javax.swing.JTextField txtLeaseLength;
	private javax.swing.JTextField txtRent;
	// End of variables declaration//GEN-END:variables
	private Tenant tenant;
	private Property property;
}
